/*
 * PieChart.java
 * Android-Charts
 *
 * Created by limc on 2011/05/29.
 *
 * Copyright 2011 limc.cn All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 *
 */
package cn.limc.ohoscharts.view;


import cn.limc.ohoscharts.entity.TitleValueColorEntity;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

import java.util.List;

/**
 *
 * <p>
 * PieChart is a kind of graph that display all in a pie-like graph, each of the
 * data will get a part of the pie. another kind of pie chart you can refer from
 * PizzaChart
 * </p>
 * <p>
 * PieChartは円グラフの一種です、分割表示をお使い場合、PizzaChartを利用してください
 * </p>
 * <p>
 * PieChart是最简单的饼图，如果您需要可以分割表示的饼图，请参照PizzaChart
 * </p>
 *
 * @author limc
 * @version v1.0 2011/05/29 15:15:03
 *
 */
public class PieChart extends RoundChart implements Component.DrawTask {

  /**
   * <p>
   * Data
   * </p>
   * <p>
   * データ
   * </p>
   * <p>
   * 图表数据
   * </p>
   */
  protected List<TitleValueColorEntity> data;

  /*
   * (non-Javadoc)
   *
   * @param context
   *
   * @see cn.limc.ohoscharts.view.AbstractBaseChart#BaseChart(Context)
   */
  public PieChart(Context context) {
    super(context);
  }

  /*
   * (non-Javadoc)
   *
   * @param context
   *
   * @param attrs
   *
   * @param defStyle
   *
   * @see cn.limc.ohoscharts.view.AbstractBaseChart#BaseChart(Context,
   * AttributeSet, int)
   */
  public PieChart(Context context, AttrSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  /*
   * (non-Javadoc)
   *
   * @param context
   *
   * @param attrs
   *
   * @see cn.limc.ohoscharts.view.AbstractBaseChart#BaseChart(Context,
   * AttributeSet)
   */
  public PieChart(Context context, AttrSet attrs) {
    super(context, attrs);
    addDrawTask(this);
  }

  /*
   * (non-Javadoc)
   *
   * <p>Called when is going to draw this chart</p> <p>チャートを書く前、メソッドを呼ぶ</p>
   * <p>绘制图表时调用</p>
   *
   * @param canvas
   *
   * @see ohos.view.View#onDraw(ohos.graphics.Canvas)
   */

  @Override
  public void onDraw(Component component, Canvas canvas) {

    int rect = super.getWidth() > super.getHeight() ? super.getHeight()
        : super.getWidth();

    drawBorder(canvas);

    // calculate radius length
    longitudeLength = (int) ((rect / 2f) * 0.90);

    // calculate position
    position = new Point((int) (getWidth() / 2f), (int) (getHeight() / 2f));

    // draw this chart
    drawCircle(canvas);

    // draw data on chart
    drawData(canvas);
  }

  /**
   * <p>
   * Draw a circle
   * </p>
   * <p>
   * 満丸を書く
   * </p>
   * <p>
   * 绘制一个圆
   * </p>
   *
   * @param canvas
   */
  protected void drawCircle(Canvas canvas) {

    Paint mPaintCircleBorder = new Paint();
    mPaintCircleBorder.setColor(new Color(circleBorderColor));
    mPaintCircleBorder.setStyle(Paint.Style.STROKE_STYLE);
    mPaintCircleBorder.setStrokeWidth(2);
    mPaintCircleBorder.setAntiAlias(true);

    // draw a circle
    canvas.drawCircle(position.getPointX(), position.getPointY(), longitudeLength,
        mPaintCircleBorder);
  }

  /**
   * <p>
   * Draw the data
   * </p>
   * <p>
   * チャートでデータを書く
   * </p>
   * <p>
   * 将数据绘制在图表上
   * </p>
   *
   * @param canvas
   */
  protected void drawData(Canvas canvas) {
    if (null != data) {
      // sum all data's value
      float sum = 0;
      for (int i = 0; i < data.size(); i++) {
        sum = sum + data.get(i).getValue();
      }

      Paint mPaintFill = new Paint();
      mPaintFill.setStyle(Paint.Style.FILL_STYLE);
      mPaintFill.setAntiAlias(true);

      Paint mPaintBorder = new Paint();
      mPaintBorder.setStyle(Paint.Style.STROKE_STYLE);
      mPaintBorder.setColor(new Color(longitudeColor));
      mPaintBorder.setAntiAlias(true);

      int offset = -90;
      // draw arcs of every piece
      for (int j = 0; j < data.size(); j++) {
        TitleValueColorEntity e = data.get(j);

        // get color
        mPaintFill.setColor(new Color(e.getColor()));

        RectFloat oval = new RectFloat(position.getPointX() - longitudeLength, position.getPointY()
            - longitudeLength, position.getPointX() + longitudeLength, position.getPointY()
            + longitudeLength);
        int sweep = Math.round(e.getValue() / sum * 360f);
        Arc arc = new Arc(offset, sweep, true);

        canvas.drawArc(oval, arc, mPaintFill);
        canvas.drawArc(oval, arc, mPaintBorder);
        offset = offset + sweep;
      }

      float sumvalue = 0f;
      for (int k = 0; k < data.size(); k++) {
        TitleValueColorEntity e = data.get(k);
        float value = e.getValue();
        sumvalue = sumvalue + value;
        float rate = (sumvalue - value / 2) / sum;
        mPaintFill.setColor(Color.BLUE);

        // percentage
        float percentage = (int) (value / sum * 10000) / 100f;

        float offsetX = (float) (position.getPointX() - longitudeLength * 0.5
            * Math.sin(rate * -2 * Math.PI));
        float offsetY = (float) (position.getPointY() - longitudeLength * 0.5
            * Math.cos(rate * -2 * Math.PI));

        Paint mPaintFont = new Paint();
        mPaintFont.setColor(Color.LTGRAY);

        // draw titles
        String title = e.getTitle();
        float realx = 0;
        float realy = 0;

        // TODO title position
        if (offsetX < position.getPointX()) {
          realx = offsetX - mPaintFont.measureText(title) - 5;
        } else if (offsetX > position.getPointX()) {
          realx = offsetX + 5;
        }

        if (offsetY > position.getPointY()) {
          if (value / sum < 0.2f) {
            realy = offsetY + 10;
          } else {
            realy = offsetY + 5;
          }
        } else if (offsetY < position.getPointY()) {
          if (value / sum < 0.2f) {
            realy = offsetY - 10;
          } else {
            realy = offsetY + 5;
          }
        }

        canvas.drawText(mPaintFont, title, realx, realy - 15);
        canvas.drawText(mPaintFont, String.valueOf(percentage) + "%", realx,
            realy + 12);
      }
    }
  }

  /**
   * @return the data
   */
  public List<TitleValueColorEntity> getData() {
    return data;
  }

  /**
   * @param data
   *            the data to set
   */
  public void setData(List<TitleValueColorEntity> data) {
    this.data = data;
  }
}
